fix: skip non-image parts when matching by sha1 in ImageParts#1566
Open
TemRevil wants to merge 1 commit into
Open
fix: skip non-image parts when matching by sha1 in ImageParts#1566TemRevil wants to merge 1 commit into
TemRevil wants to merge 1 commit into
Conversation
Package._gather_image_parts appends every part reached through an image relationship to the ImageParts collection, casting it to ImagePart. When an image uses a type that does not load as an ImagePart (for example an SVG, or an image with broken or non-standard EXIF data), the target is a plain Part with no sha1 attribute. The next call to add_picture runs ImageParts._get_by_sha1, which reads .sha1 on every collected part, so it raised "'Part' object has no attribute 'sha1'" as soon as such a part was present. Skip parts that have no sha1 attribute while matching, mirroring the same fix in python-pptx. Add a unit test covering a non-image Part placed before the matching ImagePart in the collection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1565.
Problem
Package._gather_image_partswalks the package relationships and appends the target of every image relationship to theImagePartscollection, casting it toImagePart:The cast is only a type hint. If an image uses a type that does not load as an
ImagePart(an SVG, or an image with broken or non-standard EXIF data as in the report), the target part is a plainPart, which has nosha1.Once such a part is in the collection, the next
add_picturecall runsImageParts._get_by_sha1, which reads.sha1on every collected part, so it raised'Part' object has no attribute 'sha1'.Fix
Skip parts that have no
sha1while matching:This mirrors the same fix already made in python-pptx (
_ImageParts._find_by_sha1, commit 4c0fd125), which the issue links to.Tests
Added
it_skips_parts_without_a_sha1_when_matchingintests/test_package.py. It puts a plainPart(no sha1) ahead of the matchingImagePartin the collection and asserts_get_by_sha1returns theImagePartrather than raising. The test fails on the current code with the reportedAttributeErrorand passes with the fix.Verified with
pytest tests/test_package.py(9 passed) andruff checkon the changed files. The unrelated collection failures elsewhere in the suite on my machine come from a pyparsing version mismatch in the cxml test helper and are present on a clean checkout too.